﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.vsEnv
{
    public static class Helper
    {

        public static bool TryParseIni(string text, out Dictionary<string, Dictionary<string, string>> result, out string message)
        {
            result = new Dictionary<string, Dictionary<string, string>>();
            message = null;
            bool error = false;
            using (StringReader sr = new StringReader(text))
            {
                string line;
                string path = null;
                Dictionary<string, string> variables = null;
                int lineNumber = 0;
                while (null != (line = sr.ReadLine()) && !error)
                {
                    lineNumber++;
                    if (line.StartsWith("#") || line.Trim() == "")
                    {
                        continue;
                    }
                    else if (line.StartsWith("[") && line.EndsWith("]"))
                    {
                        if (path != null)
                        {
                            if (result.ContainsKey(path))
                            {
                                error = true;
                                message = "Duplicate folder at line " + lineNumber;
                            }
                            result.Add(path, variables);
                            path = null;
                        }
                        path = line.Substring(1, line.Length - 2);
                        variables = new Dictionary<string, string>();
                    }
                    else if (line.Contains("="))
                    {
                        int mid = line.IndexOf("=");
                        string key = line.Substring(0, mid);
                        string value = line.Substring(mid + 1);
                        if (variables == null)
                        {
                            error = true;
                            message = "Can not set variable without folder. Please add a \"[folderPath]\" first";
                        }
                        else if (variables.ContainsKey(key))
                        {
                            error = true;
                            message = "Duplicate key " + key + " at line " + lineNumber;
                        }
                        else
                        {
                            variables.Add(key, value);
                        }
                    }
                    else
                    {
                        error = true;
                        message = "Invalid data on line " + lineNumber + ":" + line;
                    }
                }
                if (path != null)
                {
                    result.Add(path, variables);
                    path = null;
                }
            }

            return !error;
        }
    }
}
